﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Spelprojekt
{
    class Player
    {
        Vector2 position;
        //Texture2D Spaceship;
        Animation playerAnimation;
        int windowWidth, windowHeight;
        float velocity = 0.0f, velocity2 = 0.0f;
        float radius = 16.0f;
        bool alive = true;

        public Player(GraphicsDevice device, Vector2 position, Vector2 origin)
        {
            // The position that is passed in is now set to the position above
            this.position = position;

            playerAnimation = new Animation(position);
            // Set window dimensions
            windowHeight = device.Viewport.Height;
            windowWidth = device.Viewport.Width;

            playerAnimation.SpriteOrigin = origin;
            playerAnimation.Stop();
        }
        public Vector2 Position
        {
            set { position = value; }
            get { return position; } 
        }

        public void AddCell(Texture2D cellPicture)
        {
            playerAnimation.AddCell(cellPicture);
        }

        public void Draw(SpriteBatch batch)
        {
            playerAnimation.SetPosition(position);
            playerAnimation.Draw(batch);
        }

        public bool CollisionTest(Vector2 point)
        {
            if ((point - position).Length() < this.radius)
            {
                if (alive)
                {
                    alive = false;
                }
                return false;
            }
            else
                return true;
        }

        public bool Alive
        {
            set { alive = value; }
            get { return alive; }
        }

        public void Update(GameTime gameTime)
        {
            playerAnimation.Update(gameTime);


            // Up/Down movement
            position.Y += velocity * (float)gameTime.ElapsedGameTime.TotalSeconds;
            velocity *= 0.96f;
            // Left/Right movement
            position.X += velocity2 * (float)gameTime.ElapsedGameTime.TotalSeconds;
            velocity2 *= 0.94f;
            // Check so the ship doesn't go off-screen
            if (position.Y < 18) position.Y = 18.0f;
            if (position.Y > windowHeight - 18) position.Y = windowHeight - 18;
            if (position.X < 19) position.X = 19.0f;
            if (position.X > windowWidth - 19) position.X = windowWidth - 19;
        }
        public void Accelerate()
        {
            playerAnimation.GotoFrame(1);
            velocity -= 60.0f;
        }
        public void MoveReverse()
        {
            velocity += 30.0f;
        }
        public void TurnRight()
        {
            playerAnimation.GotoFrame(3);
            velocity2 += 30.0f;
        }
        public void TurnLeft()
        {
            playerAnimation.GotoFrame(2);
            velocity2 -= 30.0f;
        }
        public void Still()
        {
            playerAnimation.GotoFrame(0);
        }
    }
    class Fireball
    {
        Vector2 position;

        Texture2D picture;
        float speed;
        public Fireball(Texture2D firePicture, Vector2 startPosition, float
        updateSpeed)
        {
            picture = firePicture;
            position = startPosition;
            speed = updateSpeed*1.1f;
        }
        public Vector2 Position { get { return position; } }
        public void Draw(SpriteBatch batch)
        {
            batch.Draw(picture, position, null, Color.White, 0.0f, new Vector2(
            10.0f, 10.0f), 1.0f, SpriteEffects.None, 1.0f);
        }
        public void Update(GameTime gameTime)
        {
            position.Y -= speed *
            (float)gameTime.ElapsedGameTime.TotalSeconds;
        }
    }
}
